package com.hotelsystem.reservations.main;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JPanel;

import com.hotelsystem.reservation.controllers.CreateRoomController;
import com.hotelsystem.reservation.controllers.ReservationsListController;
import com.hotelsystem.reservation.controllers.RoomListController;
import com.hotelsystem.reservation.controllers.UserListController;
import com.hotelsystem.reservations.layouts.OpenUserView;
import com.hotelsystem.reservations.layouts.ReservationListView;
import com.hotelsystem.reservations.layouts.RoomListView;
import com.hotelsystem.reservations.persons.Client;

public class MainMenuController {

	private MainMenuView mainMenuView;
	private MainWindow mainWindow;
	private UserListController userListController;
	private RoomListController roomListController;
	private ReservationsListController reservationsListController;

	public MainMenuController() {
		mainMenuView = new MainMenuView();
		mainWindow = new MainWindow();
		mainWindow.setMenuBar(mainMenuView.getMainMenuBar());
	}

	public void addHomeListener() {
		mainMenuView.setHomeListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				goToHome();
			}
		});
	}

	public void addViewClientsListener() {

		mainMenuView.setViewClientsListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				userListController = new UserListController(
						MainMenuController.this);
				mainWindow.getMainFrame().getContentPane().removeAll();
				mainWindow.setJPanel(userListController.getUserListPanel());
				userListController.fillList();

			}
		});
	}

	public void addNewClientListener() {

		mainMenuView.setAddClientItemListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				Client client = new Client();
				OpenUserView openUser = new OpenUserView(client, true);
				mainWindow.setJPanel(openUser.getMainPanel());

			}
		});
	}

	public void addNewRoomListener() {
		mainMenuView.setAddRoomItemListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				CreateRoomController createRoomController = new CreateRoomController(
						MainMenuController.this);
				mainWindow.setJPanel(createRoomController.getMainPanel());

			}
		});
	}

	public void addViewReservationsListener() {

		mainMenuView.setViewReservationsListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				ReservationListView layout = new ReservationListView();
				JPanel panel = layout.getMainPanel();
				reservationsListController = new ReservationsListController(
						layout);
				mainWindow.getMainFrame().getContentPane().removeAll();
				mainWindow.setJPanel(reservationsListController
						.getReservationListView().getMainPanel());

			}
		});
	}

	public void addViewRoomListener() {

		mainMenuView.setViewRoomsListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				roomListController = new RoomListController(
						MainMenuController.this);
				mainWindow.getMainFrame().getContentPane().removeAll();
				mainWindow.setJPanel(roomListController.getRoomListPanel());
				roomListController.fillList();
			}
		});
	}

	public JPanel getMainPanel() {
		return mainWindow.getMainPanel();
	}

	public JMenuBar getMenuPanel() {
		return mainMenuView.getMainMenuBar();
	}

	// public static synchronized MainMenuController getInstance() {
	// if (instance == null) {
	// instance = new MainMenuController();
	// }
	//
	// return instance;
	// }

	public MainWindow getMainWindow() {
		return mainWindow;
	}

	public void goToHome() {
		mainWindow.getMainFrame().getContentPane().removeAll();
		mainWindow.setJPanel(mainMenuView.getHomePanel());
	}

	public static void main(String[] args) {
		MainMenuController mainMenuController = new MainMenuController();
		mainMenuController.addViewClientsListener();
		mainMenuController.addNewClientListener();
		mainMenuController.addViewRoomListener();
		mainMenuController.addViewReservationsListener();
		mainMenuController.addHomeListener();
		mainMenuController.addNewRoomListener();
	}
}
